<template>
  <div>
    <h1>BlockChainDevelop</h1>
    <div class="box">
      <div class="title">
        Admin Login
      </div>
      <el-form :model="ruleForm" status-icon ref="ruleForm" class="demo-ruleForm" label-width="100px" :rules="rules">
        <el-form-item label="账号" prop="user" style="margin-top: 30px">
          <el-input type="text" v-model="ruleForm.user" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass" style="margin-top: 30px">
          <el-input type="password" v-model="ruleForm.pass" style="width: 300px"></el-input>
        </el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" style="width: 300px;margin-left: 100px">登陆</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>

import qs from "qs"

export default {
  name: 'Login',
  data() {
    return {
      ruleForm: {
        user: '',
        pass: '',
      },
      rules: {
        user: [
          {required: true, message: "账号不能为空", trigger: 'blur'}
        ],
        pass: [
          {required: true, message: "密码不能为空", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    //实现账号提交
    submitForm(formName) {
      this.$refs[formName].validate().then(() => {
        this.$http.post("/admin/login", qs.stringify({
          username: this.ruleForm.user,
          password: this.ruleForm.pass,
        })).then((rep) => {
          if (rep.data.code == 500) {
            this.$notify({
              title: '登陆失败',
              message: rep.data.msg,
              type: 'warning'
            });
          } else if (rep.data.code == 200) {
            this.$auth.setAdminAuth(rep.data.admin_token,rep.data.username)
            this.$router.push("/admin/home")
            this.$notify({
              title: '登陆成功',
              message: rep.data.msg,
              type: 'success'
            });
          }

        }).catch((rep) => {
          //如果有错就打印错误
          console.log(rep)
        })
      })
    }
  }
}
</script>

<style scoped>
h1 {
  margin-left: 120px;
  margin-top: 70px;
  font-size: 50px;
}

.title {
  text-align: center;
  font-size: 24px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  margin-top: 20px;
}

.box {
  width: 500px;
  height: 330px;
  border: 1px solid #000;
  margin-left: 500px;
  margin-top: 50px;
  border-radius: 4px;
}

</style>
